import logging
from flask import jsonify
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from info import create_app, db
from Config import DevelopmentConfig, ProductionConfig
from info import models
from info.models import User
from info.utils.response_code import RET

app = create_app(DevelopmentConfig)
manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)


@manager.option('-n', '-name', dest='name')
@manager.option('-p', '-password', dest='password')
def creatsuperuser(name, password):
    """创建管理者账号"""
    if not all([name, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数错误')

    # 创建该模型
    user = User()
    user.mobile = name
    user.nick_name = name
    user.password = password
    user.is_admin = True
    # 提交数据库
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        logging.error(e)
        db.session.rollback()


if __name__ == '__main__':
    # print(app.url_map)
    manager.run()
